package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.dashboard.util.ConverterUtils;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 网关api流控 向naocs推送规则
 * @author cjunxian
 * @date 2021/6/25
 */
@Component("GatewayApiRuleNacosPublisher")
public class GatewayApiRuleNacosPublisher implements DynamicRulePublisher<List<ApiDefinitionEntity>> {

    @Autowired
    private ConfigService configService;

    @Autowired
    ConverterUtils converterUtils;

    @Override
    public void publish(String app, List<ApiDefinitionEntity> rules) throws Exception {

         converterUtils.pushRule(configService
                ,app
                ,NacosConfigUtil.GATEWAY_API_DATA_ID_POSTFIX
                ,NacosConfigUtil.GROUP_ID
                ,rules
                , ApiDefinitionEntity.class);

    }
}
